package com.wuwei.elearning.question.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.SelfTestAnswer;
import com.wuwei.elearning.question.mapper.SelfTestAnswerMapper;
import com.wuwei.elearning.question.mapper.SelfTestResultMapper;
import com.wuwei.elearning.utils.result.Result;
import com.wuwei.elearning.utils.token.TokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuwei
 * @since 2024-04-10
 */
@RestController
@CrossOrigin
@RequestMapping("/elearning/selfTestAnswer")
public class SelfTestAnswerController {

    @Resource
    private SelfTestAnswerMapper selfTestAnswerMapper;

    /**
     * 获取用户做的题目的所有选项的方法
     * @param selfTestId self_test表的id
     * @param request
     * @return
     */
    @GetMapping("geBySelfTestId/{selfTestId}")
    public Result<List<List<String>>> geBySelfTestId(@PathVariable("selfTestId") Long selfTestId, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        Long account = TokenUtil.getAccountFromToken(token);
        LambdaQueryWrapper<SelfTestAnswer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SelfTestAnswer::getSelfTestId,selfTestId).eq(SelfTestAnswer::getUserAccount,account);
        List<SelfTestAnswer> selfTestAnswers = selfTestAnswerMapper.selectList(queryWrapper);

        List<List<String>> result = selfTestAnswers.stream().map(t -> {
            List<String> list = new ArrayList<>();
            for (int i = 0; i < 4; i++) {
                list.add("");
            }
            String selected = t.getSelected();
            if (selected == null) {
                return list;
            } else {
                String[] split = selected.split("\\+");
                for (int i = 0; i < split.length; i++) {
                    switch (split[i]) {
                        case "A":
                            list.set(0, split[i]);
                            break;
                        case "B":
                            list.set(1, split[i]);
                            break;
                        case "C":
                            list.set(2, split[i]);
                            break;
                        case "D":
                            list.set(3, split[i]);
                            break;
                        default:
                            System.out.println("其他");
                    }
                }
                return list;
            }
        }).collect(Collectors.toList());
        return Result.ok(result);
    }

}

